from django.db.models.signals import post_save
from django.dispatch import receiver
from user_app.models import UserProfile
from vehicle_app.models import Vehicle
from .models import Review


@receiver(post_save, sender=UserProfile)
def create_user_review(sender, instance, created, **kwargs):
    """当用户提交详细资料时创建审核记录"""
    # 检查用户是否已经存在审核记录
    if not Review.objects.filter(user=instance.user, type=Review.Type.USER).exists():
        Review.objects.create(
            type=Review.Type.USER,
            user=instance.user,
            status=Review.Status.PENDING
        )


@receiver(post_save, sender=Vehicle)
def create_vehicle_review(sender, instance, created, **kwargs):
    """当车主提交车辆信息时创建审核记录"""
    # 检查车辆是否已经存在审核记录
    if not Review.objects.filter(vehicle=instance, type=Review.Type.VEHICLE).exists():
        Review.objects.create(
            type=Review.Type.VEHICLE,
            vehicle=instance,
            status=Review.Status.PENDING
        )